package com.tally.server.service;

import com.tally.server.dto.EverydayCostDTO;
import com.tally.server.dto.EverydayIncomeDTO;
import com.tally.server.enums.CostTypeEnum;
import com.tally.server.enums.IncomeTypeEnum;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class ExcelService {
    public HSSFWorkbook downloadCostExcel(List<EverydayCostDTO> list) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String[] excelHeader = new String[CostTypeEnum.values().length + 2];
        int headerCol = 1;
        excelHeader[0] = "日期";
        for (CostTypeEnum costTypeEnum: CostTypeEnum.values()) {
            excelHeader[headerCol ++] = costTypeEnum.getDesc();
        }
        excelHeader[headerCol] = "总计";
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建HSSFSheet对象
        HSSFSheet sheet = wb.createSheet("支出表");
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font = wb.createFont();
        font.setFontName("宋体");
        style.setFont(font);

        CellRangeAddress cellRangeAddress = new CellRangeAddress(0,0,0,excelHeader.length);
        sheet.addMergedRegion(cellRangeAddress);

        // 第一行
        HSSFCellStyle titleStyle = wb.createCellStyle();

        HSSFFont titleFont = wb.createFont();
        titleFont.setFontHeightInPoints((short)20);
        titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        titleStyle.setFont(titleFont);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        String titleText = sdf.format(list.get(0).getRecordDate()) + "至" + sdf.format(list.get(list.size() - 1).getRecordDate()) + "支出详情";

        HSSFCell titleCell = row.createCell(0);
        titleCell.setCellStyle(titleStyle);
        titleCell.setCellValue(titleText);

        //第二行时间和类型
        row = sheet.createRow(1);
        for (int i = 0; i < excelHeader.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(excelHeader[i]);
            cell.setCellStyle(style);
            sheet.autoSizeColumn(i);
            //设置指定列的列宽，256 * 50这种写法是因为width参数单位是单个字符的256分之一
            sheet.setColumnWidth(cell.getColumnIndex(), 100 * 50);
        }

        //中间的数据行
        double[] sums = new double[CostTypeEnum.values().length + 1];
        for (int i = 0; i < list.size(); i++) {
            int col = 0;
            row = sheet.createRow(i + 2);
            EverydayCostDTO everydayCostDTO = list.get(i);
            row.createCell(col).setCellValue(sdf.format(everydayCostDTO.getRecordDate()));
            for (CostTypeEnum costTypeEnum: CostTypeEnum.values()) {
                double money = everydayCostDTO.getTypeMoneyMap().get(costTypeEnum.getDesc()).doubleValue();
                row.createCell(++col).setCellValue(money);
                sums[col-1] += money;
            }
            row.createCell(++col).setCellValue(everydayCostDTO.getSum().doubleValue());
            sums[col - 1] += everydayCostDTO.getSum().doubleValue();
        }

        // 最后一行总和
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont sumFont = wb.createFont();
        sumFont.setColor(HSSFColor.RED.index);
        sumStyle.setFont(sumFont);
        row = sheet.createRow(list.size() + 2);
        for (int i = 0; i < sums.length; i++) {
            HSSFCell sumCell = row.createCell(i+1);
            sumCell.setCellValue(sums[i]);
            sumCell.setCellStyle(sumStyle);
        }
        return wb;
    }

    public HSSFWorkbook downloadIncomeExcel(List<EverydayIncomeDTO> list) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String[] excelHeader = new String[IncomeTypeEnum.values().length + 2];
        int headerCol = 1;
        excelHeader[0] = "日期";
        for (IncomeTypeEnum incomeTypeEnum: IncomeTypeEnum.values()) {
            excelHeader[headerCol ++] = incomeTypeEnum.getDesc();
        }
        excelHeader[headerCol] = "总计";
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建HSSFSheet对象
        HSSFSheet sheet = wb.createSheet("收入表");
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font = wb.createFont();
        font.setFontName("宋体");
        style.setFont(font);

        CellRangeAddress cellRangeAddress = new CellRangeAddress(0,0,0,excelHeader.length);
        sheet.addMergedRegion(cellRangeAddress);

        // 第一行
        HSSFCellStyle titleStyle = wb.createCellStyle();

        HSSFFont titleFont = wb.createFont();
        titleFont.setFontHeightInPoints((short)20);
        titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        titleStyle.setFont(titleFont);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        String titleText = sdf.format(list.get(0).getRecordDate()) + "至" + sdf.format(list.get(list.size() - 1).getRecordDate()) + "收入详情";

        HSSFCell titleCell = row.createCell(0);
        titleCell.setCellStyle(titleStyle);
        titleCell.setCellValue(titleText);

        //第二行时间和类型
        row = sheet.createRow(1);
        for (int i = 0; i < excelHeader.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(excelHeader[i]);
            cell.setCellStyle(style);
            sheet.autoSizeColumn(i);
            //设置指定列的列宽，256 * 50这种写法是因为width参数单位是单个字符的256分之一
            sheet.setColumnWidth(cell.getColumnIndex(), 100 * 50);
        }

        //中间的数据行
        double[] sums = new double[IncomeTypeEnum.values().length + 1];
        for (int i = 0; i < list.size(); i++) {
            int col = 0;
            row = sheet.createRow(i + 2);
            EverydayIncomeDTO everydayIncomeDTO = list.get(i);
            row.createCell(col).setCellValue(sdf.format(everydayIncomeDTO.getRecordDate()));
            for (IncomeTypeEnum incomeTypeEnum : IncomeTypeEnum.values()) {
                double money = everydayIncomeDTO.getTypeMoneyMap().get(incomeTypeEnum.getDesc()).doubleValue();
                row.createCell(++col).setCellValue(money);
                sums[col-1] += money;
            }
            row.createCell(++col).setCellValue(everydayIncomeDTO.getSum().doubleValue());
            sums[col - 1] += everydayIncomeDTO.getSum().doubleValue();
        }

        // 最后一行总和
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont sumFont = wb.createFont();
        sumFont.setColor(HSSFColor.RED.index);
        sumStyle.setFont(sumFont);
        row = sheet.createRow(list.size() + 2);
        for (int i = 0; i < sums.length; i++) {
            HSSFCell sumCell = row.createCell(i+1);
            sumCell.setCellValue(sums[i]);
            sumCell.setCellStyle(sumStyle);
        }
        return wb;
    }
}
